----------------------------------------------------------------------------------------------------------------------------------
--[[

    花朵状态切换器.

    为了方便各种冷热源 搜索，本模块也挂在玩家身上，方便搜索.

]]--
----------------------------------------------------------------------------------------------------------------------------------
--- 公共数据表
    local switch_data = {} --- 切换数据
    local hot_resource_prefab = {} --- 热资源预制体
    local cold_resource_prefab = {} --- 冷资源预制体
----------------------------------------------------------------------------------------------------------------------------------
---
    local function GetReplica(self)
        return self.inst.replica.hutao_com_flower_switcher or self.inst.replica._.hutao_com_flower_switcher
    end
    local function set_OverridePrefab(self,prefab)
        local replica = GetReplica(self)
        if replica then
            replica:SetOveridePrefab(prefab)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_flower_switcher = Class(function(self, inst)
    self.inst = inst
    inst:AddTag("_hutao_com_flower_switcher")

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}

    self.OverridePrefab = "flower"

    self:AddOnLoadFn(function(self)
        local flower_type = self:Get("flower_type")
        if flower_type then
            self:Switch(flower_type,true)
        end
    end)

    self.is_not_flower = false -- 是否不是花

end,
nil,
{
    OverridePrefab = set_OverridePrefab,
})
------------------------------------------------------------------------------------------------------------------------------
-- 是否是花
    function hutao_com_flower_switcher:SetIsFlower(flag)
        self.is_not_flower = not flag
    end
------------------------------------------------------------------------------------------------------------------------------
-- 类型切换
    function hutao_com_flower_switcher:SetTypeSwtichFn(flower_type, fn)
        switch_data[flower_type] = fn
    end
    function hutao_com_flower_switcher:Switch(flower_type,onload_flag)
        if self.inst:HasTag("player") or self.is_not_flower then
            return
        end
        if switch_data[flower_type] == nil then
            return
        end
        --- 类型切换用原实体删除方法。某些机制原实体hook了，拆掉太麻烦，重新生成切换就行了。
        if not onload_flag and self:GetType() and self:GetType() ~= flower_type then
            local x,y,z = self.inst.Transform:GetWorldPosition()
            self.inst:Remove()
            local new_flower = SpawnPrefab("flower")
            new_flower.Transform:SetPosition(x,y,z)
            if new_flower.components.hutao_com_flower_switcher then
                new_flower.components.hutao_com_flower_switcher:Switch(flower_type)
            end
            return
        end
        -- switch_data[flower_type](self.inst)
        pcall(switch_data[flower_type],self.inst)
        -- self.inst:DoTaskInTime(0,switch_data[flower_type],self.inst,0)
        self:Set("flower_type",flower_type)
        self.inst:AddTag("hutao_tag."..flower_type)
    end
    function hutao_com_flower_switcher:SwitchType(flower_type, onload_flag)
        self:Switch(flower_type,onload_flag)
    end
    function hutao_com_flower_switcher:GetType()
        return self:Get("flower_type")
    end
    function hutao_com_flower_switcher:SetOveridePrefab(prefab)
        -- self.inst:DoTaskInTime(0,function()
        --     self.OverridePrefab = prefab            
        -- end)
        if self.inst.prefab and self.inst.prefab ~= prefab then
            self.OverridePrefab = prefab
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- 冷热源
    function hutao_com_flower_switcher:AddHotSourcePrefab(prefab)
        hot_resource_prefab[prefab] = true
    end
    function hutao_com_flower_switcher:AddColdSourcePrefab(prefab)
        cold_resource_prefab[prefab] = true
    end
    local function is_hot_source_fn(target,inst)
        return hot_resource_prefab[target.prefab] == true
    end
    function hutao_com_flower_switcher:IsNearHotSource(radius,over_fn)
        local ents = FindEntity(self.inst,radius or 10,over_fn or is_hot_source_fn) or {}
        return #ents > 0
    end
    local function is_cold_source_fn(target,inst)
        return cold_resource_prefab[target.prefab] == true
    end
    function hutao_com_flower_switcher:IsNearColdSource(radius,over_fn)
        local ents = FindEntity(self.inst,radius or 10,over_fn or is_cold_source_fn) or {}
        return #ents > 0
    end
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_flower_switcher:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_flower_switcher:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_flower_switcher:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_flower_switcher:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_flower_switcher:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_flower_switcher:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_flower_switcher:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_flower_switcher:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_flower_switcher:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_flower_switcher







